package com.example.sakurasay.passwordbook.data.daos

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.example.sakurasay.passwordbook.data.entities.IndependentPasswordEntry
import kotlinx.coroutines.flow.Flow

@Dao
interface IndependentPasswordDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(entry: IndependentPasswordEntry)

    @Update
    suspend fun update(entry: IndependentPasswordEntry)

    @Delete
    suspend fun delete(entry: IndependentPasswordEntry)

    @Query("SELECT * FROM independent_passwords ORDER BY account_name ASC")
    fun getAllEntries(): Flow<List<IndependentPasswordEntry>>

    @Query("SELECT * FROM independent_passwords WHERE id = :id")
    fun getEntryById(id: Int): Flow<IndependentPasswordEntry>

    @Query("SELECT * FROM independent_passwords WHERE account_name LIKE :searchQuery OR username LIKE :searchQuery")
    fun searchPasswords(searchQuery: String): Flow<List<IndependentPasswordEntry>>
}